/*
 * $Id: AxisLabelLayer.java,v 1.1 2003/05/27 06:24:10 znamenacek Exp $
 *
 * Copyright (c) Milan Jedlicka
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */

package net.sf.vofce.canvas;

import java.awt.Graphics2D;
import java.awt.Color;

/**
 * Layer labeling horizontal and vertical axis with numbers.
 * 
 * @author Milan Jedlicka
 */
public class AxisLabelLayer implements Layer {

	/** if distance is less then this constant we use short numbers. */
	public static final int MINIMAL_MARK_DISTANCE = 20;

	/** Maximal cifer count of number on horizontal ruler. */
	public static final int MAX_HORIZONTAL_LABEL_WIDTH = 6;
	
	/** Maximal cifer count of number on vertical ruler. */
	public static final int MAX_VERTICAL_LABEL_WIDTH = 6;

	/** Distance in pixels between horizontal axis (mark) and it's label. */
	public static final int X_LABEL_AXIS_DISTANCE = 20;

	/** Distance in pixels between vertical axis (mark) and it's label. */
	public static final int Y_LABEL_AXIS_DISTANCE = 10;
	
	public static final double PRECISION = Math.pow(10, -4);
	
        /** Axis x. */
        protected Axis x;

        /** Axis y. */
        protected Axis y;

	public void setAxis(Axis x, Axis y) {
		this.x = x;
                this.y = y;
	}

	public void render(Graphics2D buffer) {
		buffer.setColor(Color.black);
		if (y.getZeroPosition() != -1) {
			drawHorizontal(buffer);
		}

		if (x.getZeroPosition() != -1) {
			drawVertical(buffer);
		}
	}

	/** Draws marked values on horizontal axis. */
	public void drawHorizontal(Graphics2D buffer) {
		int[] marks;
		double[] values;

		marks = x.getMarks();
		values = x.getValues();
		
		int axisPosition = y.getZeroPosition();
		

		for (int i = 0; i < marks.length; i++) {
			String shortNumber = getShortNumber(values[marks[i]], MAX_HORIZONTAL_LABEL_WIDTH);
		
			boolean isShortNumber = false;
			if (shortNumber != null) {
				isShortNumber = true;	
			}
		
			if (values[marks[i]] != 0f) {
				if (equals(Math.round(values[marks[i]]), values[marks[i]])) {
					if (isShortNumber) {
						buffer.drawString(shortNumber,  
							marks[i]-getXPosition((double) Math.pow(10, shortNumber.length()-1)), 
							axisPosition + X_LABEL_AXIS_DISTANCE);
					} else {
						buffer.drawString(Integer.toString((int) values[marks[i]]), 
							marks[i]-getXPosition(values[marks[i]]), 
							axisPosition + X_LABEL_AXIS_DISTANCE);
					}
				} else {
					if (isShortNumber) {
						buffer.drawString(shortNumber,  
							marks[i]-getXPosition((double) Math.pow(10, shortNumber.length()-1)),
							axisPosition + X_LABEL_AXIS_DISTANCE);	
					} else {
						buffer.drawString(Double.toString(values[marks[i]]), 
							marks[i]-getXPosition(values[marks[i]]),
							axisPosition + X_LABEL_AXIS_DISTANCE); 
					}
						
				}
				
			} else {
				buffer.drawString(Integer.toString((int) values[marks[i]]), 
						marks[i]+8, 
						axisPosition + X_LABEL_AXIS_DISTANCE);
			}
			
		}
		
	}

	/** Draws marked values on vertical axis. */
	public void drawVertical(Graphics2D buffer) {
		int[] marks;
		double[] values;

		marks = y.getMarks();
		values = y.getValues();
		
		int axisPosition = x.getZeroPosition();
	
		for (int i = 0; i < marks.length; i++) {
			String shortNumber = getShortNumber(values[marks[i]], MAX_VERTICAL_LABEL_WIDTH);
			
			boolean isShortNumber = false;
			if (shortNumber != null) {
				isShortNumber = true;	
			}

			if (values[marks[i]] != 0f) {
				if (equals(Math.round(values[marks[i]]), values[marks[i]])) {
					if (isShortNumber) {
						buffer.drawString(shortNumber,
								axisPosition + Y_LABEL_AXIS_DISTANCE, 
								marks[i]+5);	
					} else {
						buffer.drawString(Integer.toString((int) values[marks[i]]), 
								axisPosition + Y_LABEL_AXIS_DISTANCE, 
								marks[i]+5);
					}
				} else {
					if (isShortNumber) {
						buffer.drawString(shortNumber, 
								axisPosition+Y_LABEL_AXIS_DISTANCE, 
								marks[i]+5);
					} else {
						buffer.drawString(Double.toString(values[marks[i]]), 
								axisPosition+Y_LABEL_AXIS_DISTANCE, 
								marks[i]+5);	
					}
				}
			} 
		}
		
	}

	/** Converts number into short number representation. */
	public static String getShortNumber(double number, int maxLength) {
		int counter = 0;
		if (!((number != 0f) && (Double.toString(number).length() > maxLength))) {
			return null;	
		}

		if (number > 0) {
			if (Math.abs(number) > 100) {
				while (Math.abs(number) > 100) {
					counter++;
					number /= 10;
				}
			} else {
				while (Math.abs(number) < 10) {
					counter--;
					number *= 10;
				}
			}
		} else {
			if (Math.abs(number) > 100) {
				while (Math.abs(number) > 10) {
					counter++;
					number /= 10;
				}
			} else {
				while (Math.abs(number) < 1) {
					counter--;
					number *= 10;
				}
			}
		}
	
		return Integer.toString((int) number) + "E" + counter;
	
	}

	/** Counts position of value on horizontal axis. */
	protected int getXPosition(double number) {
		String numberToString = Double.toString(number);
		int ciferCount = numberToString.length();

		return Math.round((6*ciferCount/2)-6);

	}
	
	/** Comparing numbers with given precision. */
	protected boolean equals(double a, double b) {
		return Math.abs(a - b) < PRECISION;
	}

}
